package com.ruoyi.payment.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.common.seller.aop.IgnoreSeller;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.payment.constant.PaymentOptionsConstant;
import com.ruoyi.payment.domain.PayMethod;
import com.ruoyi.payment.domain.dto.QueryPaymentDTO;
import com.ruoyi.payment.mapper.PayMethodMapper;
import com.ruoyi.payment.service.PayMethodService;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 支付方法服务impl
 *
 * @author Administrator
 * @date 2023/03/10
 */
@Service
public class PayMethodServiceImpl extends ServiceImpl<PayMethodMapper, PayMethod> implements PayMethodService {

    @Override
    public TableDataInfo<PayMethod> pagePayMethod(QueryPaymentDTO queryPaymentDTO, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectPage(pageQuery.build(), this.getQueryWrapper(queryPaymentDTO)));
    }

    @Override
    @IgnoreSeller
    public boolean deleteByIds(List<Long> payMethodIds) {
        long sysCount = this.count(
            Wrappers.<PayMethod>lambdaQuery()
                .eq(PayMethod::getSysBuiltIn, PaymentOptionsConstant.YES)
                .in(PayMethod::getPayMethodId, payMethodIds)
        );
        if (sysCount > 0L) {
            throw new ServiceException("内置支付方式无法删除");
        }
        return this.removeByIds(payMethodIds);
    }

    @Override
    public boolean addPayMethod(PayMethod payMethod) {
        payMethod.setSysBuiltIn(PaymentOptionsConstant.NO);
        return this.save(payMethod);
    }

    @Override
    public boolean editPayMethod(PayMethod payMethod) {
        if (NumberUtil.equals(payMethod.getSysBuiltIn(), PaymentOptionsConstant.YES)) {
            payMethod.setPayMethodName(null);
            payMethod.setAllowDeposit(null);
            payMethod.setAllowMember(null);
            payMethod.setAllowHangbills(null);
        }
        return this.updateById(payMethod);
    }

    /**
     * 得到查询包装
     *
     * @param queryPaymentDTO 查询dto
     * @return {@link LambdaQueryWrapper}<{@link PayMethod}>
     */
    private Wrapper<PayMethod> getQueryWrapper(QueryPaymentDTO queryPaymentDTO) {
        LambdaQueryWrapper<PayMethod> queryWrapper = Wrappers.<PayMethod>lambdaQuery()
            .orderByAsc(PayMethod::getSortNum)
            .orderByDesc(PayMethod::getSysBuiltIn);
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowHangbills())) {
            queryWrapper.eq(PayMethod::getAllowHangbills, queryPaymentDTO.getAllowHangbills());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowMember())) {
            queryWrapper.eq(PayMethod::getAllowMember, queryPaymentDTO.getAllowMember());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowOtherPos())) {
            queryWrapper.eq(PayMethod::getAllowOtherPos, queryPaymentDTO.getAllowOtherPos());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowPcPos())) {
            queryWrapper.eq(PayMethod::getAllowPcPos, queryPaymentDTO.getAllowPcPos());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowDeposit())) {
            queryWrapper.eq(PayMethod::getAllowDeposit, queryPaymentDTO.getAllowDeposit());
        }
        if (StrUtil.isNotBlank(queryPaymentDTO.getPayMethodName())) {
            queryWrapper.like(PayMethod::getPayMethodName, queryPaymentDTO.getPayMethodName());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getStatus())) {
            queryWrapper.eq(PayMethod::getStatus, queryPaymentDTO.getStatus());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getSysBuiltIn())) {
            queryWrapper.eq(PayMethod::getSysBuiltIn, queryPaymentDTO.getSysBuiltIn());
        }
        return queryWrapper;
    }

    /*

     */
/**
 * 初始化支付方式
 *
 * @param sellerId
 *//*

    @Override
    public void sellerInitSystemPayment(Long sellerId) {
        List<PayMethod> sysPaymentList = this
            .list(Wrappers.<PayMethod>lambdaQuery()
                .eq(PayMethod::getSellerId, sellerId)
                .eq(PayMethod::getSysBuiltIn, PaymentOptionsConstant.YES));
        if (PaymentSystemConstant.SYSTEM_PAYMENT_TOTAL != sysPaymentList.size()) {
            List<PayMethod> list = this.list(
                Wrappers.<PayMethod>lambdaQuery()
                    .isNull(PayMethod::getSellerId)
                    .eq(PayMethod::getSysBuiltIn, PaymentOptionsConstant.YES)
                    .notIn(PayMethod::getPayMethodType,
                        sysPaymentList.stream().map(PayMethod::getPayMethodType))
            );
            list.forEach(n -> {
                n.setSellerId(sellerId);
                n.setPayMethodId(null);
            });
            this.saveBatch(list);
        }
    }


    @Override
    public PageUtils queryPage(QueryPaymentDTO queryPaymentDTO) {
        IPage<PayMethod> page = this.page(
            new Query<PayMethod>().getPage(queryPaymentDTO),
            this.getQueryWarapper(queryPaymentDTO));
        return new PageUtils(page);
    }


    */
/**
 * 查询支付方式
 *
 * @param queryPaymentDTO
 * @return
 *//*

    @Override
    public List<PayMethod> listBySellerId(QueryPaymentDTO queryPaymentDTO) {
        List<PayMethod> list = this.list(this.getQueryWarapper(queryPaymentDTO));
        return list;
    }

    */
/**
 * 批量删除
 *
 * @param ids
 *//*

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
        Assert.notEmpty(ids, "请选择要删除的支付方式");
        int sysCount = this.count(
            Wrappers.<PayMethod>lambdaQuery()
                .eq(PayMethod::getSysBuiltIn, PaymentOptionsConstant.YES)
                .in(PayMethod::getPayMethodId, ids)
        );
        if (sysCount > 0) {
            throw new RRException("内置支付方式无法删除");
        }
        this.removeByIds(ids);
    }

    */
/**
 * 构建查询条件
 *
 * @param queryPaymentDTO
 * @return
 *//*

    private LambdaQueryWrapper<PayMethod> getQueryWarapper(QueryPaymentDTO queryPaymentDTO) {
        LambdaQueryWrapper<PayMethod> queryWrapper = Wrappers.<PayMethod>lambdaQuery()
            .eq(PayMethod::getSellerId, queryPaymentDTO.getSellerId())
            .orderByAsc(PayMethod::getSortNum)
            .orderByDesc(PayMethod::getSysBuiltIn);
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowHangbills())) {
            queryWrapper.eq(PayMethod::getAllowHangbills, queryPaymentDTO.getAllowHangbills());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowMember())) {
            queryWrapper.eq(PayMethod::getAllowMember, queryPaymentDTO.getAllowMember());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowOtherPos())) {
            queryWrapper.eq(PayMethod::getAllowOtherPos, queryPaymentDTO.getAllowOtherPos());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowPcPos())) {
            queryWrapper.eq(PayMethod::getAllowPcPos, queryPaymentDTO.getAllowPcPos());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getAllowDeposit())) {
            queryWrapper.eq(PayMethod::getAllowDeposit, queryPaymentDTO.getAllowDeposit());
        }
        if (StringUtils.isNotBlank(queryPaymentDTO.getKey())) {
            queryWrapper.like(PayMethod::getPayMethodName, queryPaymentDTO.getKey());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getStatus())) {
            queryWrapper.eq(PayMethod::getStatus, queryPaymentDTO.getStatus());
        }
        if (ObjUtil.isNotNull(queryPaymentDTO.getSysBuiltIn())) {
            queryWrapper.eq(PayMethod::getSysBuiltIn, queryPaymentDTO.getSysBuiltIn());
        }
        return queryWrapper;
    }


    @Override
    public String getNameById(Long paymentId) {
        if (paymentId == null) {
            return null;
        }
        PayMethod PayMethod = this.getById(paymentId);
        return PayMethod == null ? null : PayMethod.getPayMethodName();
    }

    */
/**
 * 修改支付方式
 *
 * @param storePayMethod
 *//*

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePayment(PayMethod storePayMethod) {
        Assert.notNull(storePayMethod, "修改支付方式数据不能为空");
        if (Objects.equals(storePayMethod.getSysBuiltIn(), PaymentOptionsConstant.YES)) {
            storePayMethod.setPayMethodName(null);
            storePayMethod.setAllowDeposit(null);
            storePayMethod.setAllowMember(null);
            storePayMethod.setAllowHangbills(null);
        }
        this.updateById(storePayMethod);
    }

    */
/**
 * 新增自定义支付方式
 *
 * @param storePayMethod
 * @param sellerId
 * @param userId
 *//*

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePayment(PayMethod storePayMethod, Long sellerId, Long userId) {
        storePayMethod.setSellerId(sellerId)
            .setSysBuiltIn(PaymentOptionsConstant.NO);
        this.save(storePayMethod);
    }
*/

}
